package tree.easy;

import tree.TreeNode;

/**
 * 108. 将有序数组转换为二叉搜索树
 */
public class sortedArrayToBST {
    public static void main(String[] args) {
        int[] nums = {-10, -3, 0, 5, 9};
        new sortedArrayToBST().test(nums);
    }

    public void test(int[] nums) {
        System.out.println(new sortedArrayToBST().myCode(nums, 0, nums.length - 1));
    }

    public TreeNode myCode(int[] nums, int l, int r) {
        if (l > r) {
            return null;
        }
        int mid = (r + l) / 2;
        TreeNode treeNode = new TreeNode(nums[mid]);
        treeNode.left = myCode(nums, l, mid - 1);
        treeNode.right = myCode(nums, mid + 1, r);
        return treeNode;
    }

}
